package com.situ.java.service;

import com.situ.java.domain.Book;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class BookListService implements Serializable{
    /** serialVersionUID*/  
    private static final long serialVersionUID = 51161L;
    
    static Book[] books = new Book[1024];// 图书数组，暂时设置上限为1024本
    static int totalBook = 0;
    
    

    public List<Book> getBookList(){
        List<Book> books = new ArrayList<>();
        for (int i = 0; i < totalBook; i++) {
            books.add(this.books[i]);
        }
        return books;
    }


    public Book[] getBooks(){
        Book[] books = new Book[totalBook];// 返回存在的图书
        for (int i = 0; i < books.length; i++) {
            books[i] = this.books[i];
        }
        return books;
    }


    public void addBook(Book b) throws MyException {
        for (int i = 0; i < totalBook; i++) {
            if (b.getId()==books[i].getId())
                throw new MyException("该图书ID已存在，无法添加");
        }
        if (b instanceof Book)
            books[totalBook++] = b;
    }

}
